var scenicId, backUserId, auditStatus, provinceId, cityId, districtId;
$(function() {
	scenicId = ly.getUrlParam('scenicId') || '';
	auditStatus = ly.getUrlParam('auditStatus') || '';
	if (auditStatus == 1 || auditStatus == 2) {
		$('#save_btn').hide();
		$('#Re-apply').show();
	} else {
		$('#save_btn').show();
		$('#Re-apply').hide();
	}
	if (scenicId) {
		$('.title span,title').html('编辑');
		request({
			url: site.gateUrl + '/back/scenicManagement/selectScenicInfoViewByIdAndAuditStatus',
			data: { scenicId: scenicId, auditStatus: auditStatus },
			ajaxform: true,
			type: 'get',
			callback: function(r) {
				if (r.code == 'rest.success') {
					var d = r.result;
					$('#provincedata').val(d.provinceId);
					$('#citydata').val(d.cityId);
					$('#districtId').val(d.districtId);
					provinceId = d.provinceId;
					cityId = d.cityId;
					districtId = d.districtId;
					$('#account').val(d.account);
					// $("#adminName").val(d.adminName)
					if (d.equipmentNumber) {
						var equipmentNumber = d.equipmentNumber.split(',');
						var html = '';
						for (var index = 0; index < equipmentNumber.length; index++) {
							html += [
								'<li>',
								'<input class="ipt" placeholder="请输入设备号" value="' +
									equipmentNumber[index] +
									'" type="text">',
								'<i class="iconfont icon-remove" onclick="del(this)"></i>',
								'</li>'
							].join('');
						}
						$('#addDom').append(html);
					}
					$('#emergency').prop('checked', d.emergency == '1').val(d.emergencyId);
					$('#emergencyId').html(d.emergencyId);
					if (d.emergency == '1') {
						$('#scenicName').attr('disabled', true);
						// $('#makeupCo').val(d.scenicName);
						// $('#corpName').attr('disabled', true);
					}
					$('#password').val(d.password);
					$('#scenicName').val(d.scenicName);
					$('#corpName').val(d.corpName);
					$('#phone').val(d.phone);
					// $("#identityCard").val(d.identityCard)
					$('#sexDetails').text(d.genderDisplay);
					$('#maximumCapacityForTheDay').val(d.maximumCapacityForTheDay);
					getProvince();
					if (location.pathname.indexOf('/message/scenic/add-scenic') > -1) {
						getSources(d.scenicName);
					} else {
						$('input').attr('disabled', true);
					}
					if (d.backUserId != undefined && d.backUserId != null && d.backUserId != '') {
						backUserId = d.backUserId;
					}

					// $("#sex").getSelect({
					// 	type: "radio",
					// 	asName: "sex",
					// 	list: [
					// 		{ value: "1", display: "男" },
					// 		{ value: "2", display: "女" }
					// 	],
					// 	default: d.gender
					// });
				} else {
					ly.toast(r.msg);
				}
			}
		});
	} else {
		getProvince();
		getSources();
		// $("#sex").getSelect({
		// 	type: "radio",
		// 	asName: "sex",
		// 	list: [
		// 		{ value: "1", display: "男" },
		// 		{ value: "2", display: "女" }
		// 	],
		// 	default: 1
		// });
	}
});

//提交回到列表页
$('body').on('click', '#save_btn', function() {
	event.preventDefault();
	var account = $('#account').val();
	// var adminName=$("#adminName").val()
	var password = $('#password').val();
	var scenicName = $('#scenicName').val();
	var corpName = $('#corpName').val();
	var phone = $('#phone').val();
	// var identityCard=$("#identityCard").val()
	// var gender=$("#sex input[name=sex]:checked").val();
	var districtId = $('#districtId').val();
	var maximumCapacityForTheDay = $('#maximumCapacityForTheDay').val();
	if (account == '') {
		ly.toast('请输入文旅资源账号');
	}
	// if(adminName==''){
	// 	ly.toast('请输入文旅资源管理员姓名');
	// 	return false
	// }
	if (password == '') {
		ly.toast('请输入账号密码');
		return false;
	}
	if (scenicName == '') {
		ly.toast('请输入文旅资源名字');
		return false;
	}
	if (corpName == '') {
		ly.toast('请输入公司名字');
		return false;
	}
	// if(phone==''){
	// 	ly.toast('请输入联系电话');
	// 	return false
	// }
	// if(identityCard==''){
	// 	ly.toast('请输入身份证号');
	// 	return false
	// }
	if (maximumCapacityForTheDay == '') {
		ly.toast('请输入当日最大承载量');
		return false;
	}
	// if(gender==''){
	// 	ly.toast('请选择性别');
	// 	return false
	// }
	if (districtId == '') {
		ly.toast('请选择区域');
		return false;
	}
	var add_url;
	if (auditStatus == 0) {
		add_url = 'scenic-list.html';
	} else if (auditStatus == 1) {
		add_url = 'scenic-passed.html';
	} else if (auditStatus == 2) {
		add_url = 'scenic-rejected.html';
	}
	var equipmentList = $('#addDom').find('input');
	var equipmentNumber = [];
	for (var index = 0; index < equipmentList.length; index++) {
		if (equipmentList.eq(index).val()) {
			equipmentNumber.push(equipmentList.eq(index).val());
		}
	}
	$('.form').submitForm(
		{
			account: account,
			// adminName: adminName,
			backUserId: backUserId,
			corpName: corpName,
			districtId: districtId,
			// gender: gender,
			// identityCard:identityCard,
			password: password,
			phone: phone,
			scenicId: scenicId,
			unitType: 1,
			equipmentNumber: equipmentNumber.join(','),
			emergency: $('#emergency').prop('checked') ? '1' : '0',
			emergencyId: $('#emergency').val() ? ($('#emergency').val() == 'on' ? '' : $('#emergency').val()) : '',
			maximumCapacityForTheDay: maximumCapacityForTheDay,
			scenicName: scenicName,
			url: site.gateUrl + '/back/scenicManagement/insertOrUpdateScenicInfo',
			ajaxform: false
		},
		add_url
	); //成功后跳转的页面,可为空
});
//重新申请
$('body').on('click', '#Re-apply', function() {
	event.preventDefault();
	var account = $('#account').val();
	// var adminName=$("#adminName").val()
	var password = $('#password').val();
	var scenicName = $('#scenicName').val();
	var corpName = $('#corpName').val();
	var phone = $('#phone').val();
	// var identityCard=$("#identityCard").val()
	// var gender=$("#sex input[name=sex]:checked").val();
	var districtId = $('#districtId').val();
	var maximumCapacityForTheDay = $('#maximumCapacityForTheDay').val();
	if (account == '') {
		ly.toast('请输入文旅资源账号');
	}
	// if(adminName==''){
	// 	ly.toast('请输入文旅资源管理员姓名');
	// 	return false
	// }
	if (password == '') {
		ly.toast('请输入账号密码');
		return false;
	}
	if (scenicName == '') {
		ly.toast('请输入文旅资源名字');
		return false;
	}
	if (corpName == '') {
		ly.toast('请输入公司名字');
		return false;
	}
	// if(phone==''){
	// 	ly.toast('请输入联系电话');
	// 	return false
	// }
	// if(identityCard==''){
	// 	ly.toast('请输入身份证号');
	// 	return false
	// }
	if (maximumCapacityForTheDay == '') {
		ly.toast('请输入当日最大承载量');
		return false;
	}
	// if(gender==''){
	// 	ly.toast('请选择性别');
	// 	return false
	// }
	if (districtId == '') {
		ly.toast('请选择区域');
		return false;
	}
	var add_url;
	if (auditStatus == 0) {
		add_url = 'scenic-list.html';
	} else if (auditStatus == 1) {
		add_url = 'scenic-passed.html';
	} else if (auditStatus == 2) {
		add_url = 'scenic-rejected.html';
	}
	var equipmentList = $('#addDom').find('input');
	var equipmentNumber = [];
	for (var index = 0; index < equipmentList.length; index++) {
		if (equipmentList.eq(index).val()) {
			equipmentNumber.push(equipmentList.eq(index).val());
		}
	}
	$('.form').submitForm(
		{
			account: account,
			// adminName: adminName,
			backUserId: backUserId,
			corpName: corpName,
			districtId: districtId,
			// gender: gender,
			// identityCard:identityCard,
			password: password,
			phone: phone,
			scenicId: scenicId,
			scenicName: scenicName,
			unitType: 1,
			equipmentNumber: equipmentNumber.join(','),
			emergency: $('#emergency').prop('checked') ? '1' : '0',
			emergencyId: $('#emergency').val() ? ($('#emergency').val() == 'on' ? '' : $('#emergency').val()) : '',
			maximumCapacityForTheDay: maximumCapacityForTheDay,
			url: site.gateUrl + '/back/scenicManagement/reapplyScenicInfo',
			ajaxform: false
		},
		add_url
	); //成功后跳转的页面,可为空
});
//查询省份
function getProvince() {
	var urls = '' + site.gateUrl + '/back/areaManagement/selectAreaProvince';
	request({
		url: urls,
		data: {},
		ajaxform: true,
		type: 'post',
		callback: function(r) {
			if (r.code == 'rest.success') {
				var lists = r.result;
				var htmls = '';
				for (let i = 0; i < lists.length; i++) {
					console.log(lists[i].id, provinceId, 'lists[i].id==provinceId');
					if (lists[i].id == provinceId) {
						getcity();
						htmls +=
							'<option selected = "selected" value="' + lists[i].id + '">' + lists[i].name + '</option>';
					} else {
						htmls += '<option value="' + lists[i].id + '">' + lists[i].name + '</option>';
					}
				}
				$('#provincedata').append(htmls);
			} else {
			}
		}
	});
}
// 通过省份查询市
function getcity() {
	var id = $('#provincedata').val() || provinceId;
	console.log(id, 'getcity');
	if (id) {
		var urls = '' + site.gateUrl + '/back/areaManagement/selectAreaCityByProvinceId';
		request({
			url: urls,
			data: { provinceId: id },
			ajaxform: true,
			type: 'post',
			callback: function(r) {
				if (r.code == 'rest.success') {
					var lists = r.result;
					var htmls = '';
					for (let i = 0; i < lists.length; i++) {
						console.log(lists[i].id, cityId, 'lists[i].id==cityId');
						if (lists[i].id == cityId) {
							htmls +=
								'<option selected = "selected" value="' +
								lists[i].id +
								'">' +
								lists[i].name +
								'</option>';
							getArea();
						} else {
							htmls += '<option value="' + lists[i].id + '">' + lists[i].name + '</option>';
						}
					}
					$('#citydata').append(htmls);
				} else {
				}
			}
		});
	}
}
//通过市查询区县
function getArea() {
	var id = $('#citydata').val() || cityId;
	if (id) {
		var urls = '' + site.gateUrl + '/back/areaManagement/selectAreaDistrictByCityId';
		request({
			url: urls,
			data: { cityId: id },
			ajaxform: true,
			type: 'post',
			callback: function(r) {
				if (r.code == 'rest.success') {
					var lists = r.result;
					var htmls = '';
					for (let i = 0; i < lists.length; i++) {
						if (lists[i].id == districtId) {
							htmls +=
								'<option selected = "selected" value="' +
								lists[i].id +
								'">' +
								lists[i].name +
								'</option>';
						} else {
							htmls += '<option value="' + lists[i].id + '">' + lists[i].name + '</option>';
						}
					}
					$('#districtId').append(htmls);
				} else {
				}
			}
		});
	}
}

function add() {
	var html = [
		'<li>',
		'<input class="ipt" placeholder="请输入设备号" type="text">',
		'<i class="iconfont icon-remove" onclick="del(this)"></i>',
		'</li>'
	].join('');
	$('#addDom').append(html);
}
function del(that) {
	$(that).closest('li').remove();
}
function getSources(name) {
	var urls = site.gateUrl + '/back/scenicManagement/emergencyResource';
	request({
		url: urls,
		data: {},
		ajaxform: true,
		type: 'get',
		callback: function(r) {
			if (r.code == 'Socket.is.TimeOut') {
				ly.alert(r.desc);
				return false;
			}
			if (r.status == 0) {
				var lists = r.data;
				var htmls = '';
				TempArr = lists;
				for (let i = 0; i < lists.length; i++) {
					if (lists[i].name == name) {
						htmls +=
							'<option data-id="' +
							lists[i] +
							'" selected="selected" value="' +
							name +
							'">' +
							name +
							'</option>';
					} else {
						htmls +=
							'<option data-id="' +
							lists[i] +
							'" value="' +
							lists[i].name +
							'">' +
							lists[i].name +
							'</option>';
					}
				}
				$('#resources').find('select').append(htmls);
			}
		}
	});
}
